<?php 

class Visiteur
{
	private  $connected;
	private   $ip;
	private   $host;
	private  $userid;
	private   $browser;
	
	public function __construct() 
	{	
		if(!isset($_SESSION["connected"]))
		{
			$this->connected = false;
			$this->ip = $_SERVER["REMOTE_ADDR"];
			$this->host = gethostbyaddr($this->ip);
			$this->userid= -1;
			$this->user = false;
			$this->browser = $_SERVER['HTTP_USER_AGENT'];
		}
		else
		{
			$this->connected = true;
			$this->ip = $_SERVER["REMOTE_ADDR"];
			$this->host = gethostbyaddr($this->ip);
			$this->userid= $_SESSION["userid"];
		}
	}
	
	function isConnected()
	{
		return $this->connected == true ? true : false ;
	}
	
	function setDisconnected()
	{
		 $this->connected = false ;
		 $this->userid = -1 ;
	}
	
	function getSESSID()
	{
		return session_id();
	}
	
	function getUser()
	{
		if($this->isConnected())
		{
			$user = new User();
			$user->getByKey($this->userid);
			//debug($user);
			return $user;
		}
		else
		{
			return false;
		}
	}
	
	function canLoginWithCookie()
	{
		if (isset($_COOKIE[COOKIE_SESSIONKEY])) //COOKIE_SESSIONKEY defini dans le .conf du site
		{
			return true;
		}
		else return false;
	}
	
	
	function loginWithCookie()
	{
		$sessionkey = $_COOKIE[COOKIE_SESSIONKEY]; 			//Clé de session du cookie
		$user = new User(); 								// Utilisateur de la session
			if($user->getByKey($sessionkey, "sessionkey"))
			//--- Si on peut trouver un utilisateur ayant cette sessionken = OK
			{
				$_SESSION["connected"] = true;
				$_SESSION["userid"] = $user->id;
				$this->userid = $user->id;
				$this->connected = true;
				return true;
			}
			//--- Sinon ben on fait rien hein...
			else return false;
	}

	
	
	function login($login, $password, $rememberMe = false)
	{
		//-- On créer l'objet user qui va servir à logguer
		$tmpUser = new User();
		
		//-- on recupere le user par le login fourni
		if(!$tmpUser->getByUsername($login))
		{
			return ERR_BAD_LOGIN;
		}
		else
		{	
			
			$postKey = sha_keyCrypt($login , $password);
			//echo "PASS crypt: ".$postKey ;
			//echo "<br>PASS    DB: ".$tmpUser->sha_pass_hash ;
			
			if(strtoupper($postKey) != strtoupper($tmpUser->sha_pass_hash))
			//-- Si les 2 MDP ne concordent pas.
			{
				return ERR_BAD_PASSWORD ;
			}
			else
			{ //--- MDPs OK :  On met les infos utiles en session
				$_SESSION["connected"] = true;
				$_SESSION["userid"] = $tmpUser->id;
				$this->userid = $tmpUser->id;
				$this->connected = true;
				
				//on met à jour la DB avec la NOUVELLE sessionkey
				$tmpUser->lock();
				$tmpUser->sessionkey = $this->generateSessionKey();
				$tmpUser->unlock_attribute("sessionkey");
				$tmpUser->write();
				
				//-- On créée le cookie valable 24H :
				setcookie(COOKIE_SESSIONKEY, $tmpUser->sessionkey, time() + 86400, "/", ".killingspree.fr");
				
				return USER_CONNECTED;
			}
		}
	}
	
	function generateSessionKey()
	{
		$key = strtoupper(sha1(strtoupper($_SERVER["REMOTE_ADDR"].":".time()))) ;
		return $key;
	}
	

	function hasIE()
	{
		if(preg_match("#MSIE#", $this->browser)) return true;
		else return false;
	}
	
	function ieVersion()
	{
		$version = preg_match("/MSIE (.*).0;/", $this->browser, $regs);
		return $regs[1];
	}
}



?>